package tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class TabIO {

	public static final String TAB_FILE_EXTENSION = ".ttt";
	public static final String TABLIB_FILE = "tablib.tlb";
		
	public static boolean writeTabFile(String file, Tab t)	{
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		
		try
		{
			fos = new FileOutputStream(file);
			out = new ObjectOutputStream(fos);
			out.writeObject(t);
			out.close();
			return true;
		} catch(IOException ex)	{
			ex.printStackTrace();
			return false;
		}
	}	

	public static Tab readTabFile(String file)	{
		FileInputStream fos = null;
		ObjectInputStream in = null;
		Tab tab;

		try	{
			fos = new FileInputStream(file);
			in = new ObjectInputStream(fos);
			tab = (Tab)in.readObject();
			in.close();
			return tab;
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch(FileNotFoundException e) {
			System.err.println("Error Reading " + file + " File Not Found!");
			e.printStackTrace();
			return null;
		} catch(IOException e) {
			System.err.println("Invalid tab file!");
			e.printStackTrace();
			return null;
		}
	}	

	public static boolean writeTabLibFile(String file, TabLibrary t)	{
		FileOutputStream fos = null;
		ObjectOutputStream out = null;
		
		try
		{
			fos = new FileOutputStream(file);
			out = new ObjectOutputStream(fos);
			out.writeObject(t);
			out.close();
			return true;
		} catch(IOException ex)	{
			ex.printStackTrace();
			return false;
		}
	}	

	public static TabLibrary readTabLibFile(String file)	{
		FileInputStream fos = null;
		ObjectInputStream in = null;
		TabLibrary tabLib;

		try	{
			fos = new FileInputStream(file);
			in = new ObjectInputStream(fos);
			tabLib = (TabLibrary)in.readObject();
			in.close();
			return tabLib;
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch(FileNotFoundException e) {
			System.err.println("Error Reading " + file + " File Not Found!");
			e.printStackTrace();
			return null;
		} catch(IOException e) {
			System.err.println("Invalid tab library file!");
			e.printStackTrace();
			return null;
		}
	}	
	
	
	public static boolean hasDirectory(String dir)	{
		File f = new File(dir);
		return f.exists();
	}

	public static boolean createDirectory(String dir)	{
		File f = new File(dir);
		System.out.println("Creating directory: " + dir);
		return f.mkdir();
	}
	
	public static boolean fileExists(String file)	{
		File f = new File(file);
		return f.exists();
	}
	
	public static String getTab(String address)	{
		String input = "";
		
		if( address.startsWith("http") )
			input = fetchInput(address);
		else	{
			System.out.println("Error getting tab");
			return "Error getting tab from: " + address;
		}
		
		String tmp = extractTab(input);
		String tabText = WebGet.stripHTML(tmp);
				
		return tabText;
	}
		
	// web version of readInput
	public static String fetchInput(String iNetAddr)	{
		return WebGet.getPage(iNetAddr);
	}
	
	public static String extractTab(String input)	{		
		int index = input.lastIndexOf("<pre>") + 5;
		int lastIndex = input.lastIndexOf("</pre>");
		
		String tabText = input.substring(index, lastIndex);
		
		return tabText;
	}

}
